<template>
  <div class="layout-container">
    <el-container>
      <!-- Sidebar -->
      <el-aside :width="isCollapse ? '64px' : '200px'" class="aside">
        <div class="logo-container">
          <span v-if="!isCollapse">Weathering With You</span>
          <el-icon v-else><Monitor /></el-icon>
        </div>
        <el-menu
          :default-active="activeMenu"
          class="el-menu-vertical"
          :collapse="isCollapse"
          background-color="#304156"
          text-color="#fff"
          active-text-color="#409EFF"
          router
        >
          <el-menu-item index="/login">
            <el-icon><User /></el-icon>
            <template #title>login</template>
          </el-menu-item>
          <el-menu-item index="/home">
            <el-icon><House /></el-icon>
            <template #title>home</template>
          </el-menu-item>
          <el-menu-item index="/weatherSet">
            <el-icon><Setting /></el-icon>
            <template #title>weatherSet</template>
          </el-menu-item>
          <el-menu-item index="/geometry">
            <el-icon><Setting /></el-icon>
            <template #title>geometry</template>
          </el-menu-item>
          <el-menu-item index="/object">
            <el-icon><Setting /></el-icon>
            <template #title>object</template>
          </el-menu-item>
          <el-menu-item index="/camera">
            <el-icon><Setting /></el-icon>
            <template #title>camera</template>
          </el-menu-item>
          <el-menu-item index="/capture">
            <el-icon><Setting /></el-icon>
            <template #title>capture</template>
          </el-menu-item>
        </el-menu>
      </el-aside>

      <!-- Main Content -->
      <el-container>
        <el-header class="header">
          <div class="header-left">
            <el-icon
              class="collapse-btn"
              @click="toggleCollapse"
            >
              <Fold v-if="!isCollapse"/>
              <Expand v-else/>
            </el-icon>
          </div>
          <div class="header-right">
            <span class="user-info" @click="handleLogout">
              Logout
            </span>
          </div>
        </el-header>

        <el-main>
          <slot></slot>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useStore } from 'vuex'
import { House, User, Monitor, Fold, Expand, Setting } from '@element-plus/icons-vue'
import { ElMessageBox } from 'element-plus'

const route = useRoute()
const router = useRouter()
const store = useStore()

const isCollapse = ref(false)
const activeMenu = computed(() => route.path)

const toggleCollapse = () => {
  isCollapse.value = !isCollapse.value
}

const handleLogout = () => {
  ElMessageBox.confirm('Are you sure you want to logout?', 'Warning', {
    confirmButtonText: 'Confirm',
    cancelButtonText: 'Cancel',
    type: 'warning'
  }).then(() => {
    store.commit('user/SET_TOKEN', null)
    router.push('/login')
  })
}
</script>

<style scoped>
.layout-container {
  height: 100vh;
}

.el-container {
  width: 100vw;
  height: 100vh;
}

.aside {
  background-color: #304156;
  transition: width 0.3s;
  overflow: hidden;
}

.logo-container {
  height: 60px;
  line-height: 60px;
  text-align: center;
  color: #fff;
  font-size: 16px;
  background-color: #2b3649;
}

.el-menu-vertical {
  border-right: none;
}

.el-menu-vertical:not(.el-menu--collapse) {
  width: 200px;
}

.header {
  background-color: #fff;
  border-bottom: 1px solid #e6e6e6;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
}

.collapse-btn {
  font-size: 20px;
  cursor: pointer;
}

.user-info {
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 4px;
}

.el-main {
  background-color: #f0f2f5;
  padding: 20px;
}
</style>
